import ExamStatus from '@/components/type-of-exam/types/exam-status';
import { QuestDefaultApi } from '@/library/util/api';
import { defaultErrorHandler } from '@/library/util/error-handler';
import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { QuestionResult } from '../answer/types';
import { useLoginInforStore } from '../login';
import ContentAdapter from './adpater/ContentAdapter';
import QuestAdapter from './adpater/QuestAdapter';
import { useDesignContentStore } from './content';
import { useDesignSliderStore } from './slider';
interface DesignEditState {
  /** 编辑问卷的id */
  queid: string;

  /** 加载状态 */
  isLoading: boolean;

  isEditable: boolean;
}
export const useDesignEditStore = defineStore({
  id: 'DesignEditStore',
  state: (): DesignEditState => ({
    queid: '',
    isLoading: false,
    isEditable: false,
  }),
  actions: {
    setQuestId(value: string) {
      this.queid = value;
    },

    /** 编辑时请求问卷信息 */
    async queryDetails() {
      try {
        if (!this.queid) {
          throw new Error('请刷新重试');
        }
        this.isLoading = true;
        const result =
          (await QuestDefaultApi.questionaireQuestionaireDetailsGet({
            queId: this.queid,
          })) as QuestionResult;

        if (result) {
          const contentStore = useDesignContentStore();
          contentStore.setContent(
            new QuestAdapter().composeDetails(result, ExamStatus.Basic),
          );

          contentStore.setQuestionaire(new QuestAdapter().compose(result));

          console.log(contentStore.getQuestionaire);
        }
      } catch (error) {
        defaultErrorHandler(error);
      }
    },

    /**
     * 初始化编辑
     * @param queid
     */
    async init(queid: string) {
      try {
        this.isLoading = true;

        this.setEdit(true);

        this.setQuestId(queid);

        await this.queryDetails();

        const sliderStore = useDesignSliderStore();
        const contentStore = useDesignContentStore();

        if (!contentStore.questionaire.queType) {
          throw new Error('未找到问卷信息，请刷新重试！');
        }

        await sliderStore.getExamType(
          String(contentStore.questionaire.queType + 1),
        );
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    /** 编辑保存 */
    async queryEdit() {
      try {
        this.isLoading = true;
        const { getUserInfo } = useLoginInforStore();
        const { getQuestionaire, getContentList } = useDesignContentStore();
        console.log(getQuestionaire);
        await QuestDefaultApi.questionaireEditPost(
          new ContentAdapter().parse(
            getQuestionaire,
            getContentList,
            getUserInfo.userId,
          ),
        );
        message.success('编辑成功');
        // if (result) {
        //   message.success('编辑成功');
        // }
      } catch (error) {
        defaultErrorHandler(error);
      } finally {
        this.isLoading = false;
      }
    },

    setEdit(value: boolean) {
      this.isEditable = value;
    },
  },
});
